package com.guodexiain.nyiotclient.controller;

import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 类说明: <br>
 * <a href="http://www.xuejiaao.com">北京学佳澳教育科技有限公司</a>
 *
 * @author 作者:南阳德刚<br>
 * 2024-10-16 8:52
 */
@Controller
@RequestMapping("/iot/")
public class IotController {
    @Autowired
   private IoTDAClient client;

    @RequestMapping("/list") // list 查找设备状态
    @ResponseBody
    public ListDevicesResponse listDev() {
        // 实例化请求对象
        ListDevicesRequest request = new ListDevicesRequest();
        try {
            ListDevicesResponse response = client.listDevices(request);
            System.out.println(response.toString());
            return response;
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return null;
    }

    @RequestMapping("/cmd")//cmd
    @ResponseBody
    public CreateCommandResponse sendCmd() {
        // 实例化请求对象
        CreateCommandRequest request = new CreateCommandRequest();
        request.withDeviceId("smart_ny");       //设备ID smart_ny
        DeviceCommandRequest body = new DeviceCommandRequest(); //命令请求体
        body.setServiceId("ckzt");         //服务ID   service01
        body.setCommandName("ringAlarm");      //命令名称  switch_led
        body.withParas("{\"温度\":80}");
        request.withBody(body);


        try {
            CreateCommandResponse response = client.createCommand(request);
            System.out.println(response.toString());
            return response;
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return null;
    }
    //http://locahost:8080/iot/cmd
    //http://localhost:8080/iot/properties?service_id=dpzt&device_id=smart_ny
    //以上网址为特定网址
    @RequestMapping("/properties")
    @ResponseBody
    public ListPropertiesResponse shebeishuxing(@RequestParam String service_id, String device_id) {
        // 实例化请求对象
        ListPropertiesRequest request = new  ListPropertiesRequest();
        request.setDeviceId("smart_ny");
        request.withServiceId("dpzt"); //服务ID   车辆状态
        DeviceCommandRequest body = new DeviceCommandRequest();
        body.setServiceId("dpzt");//服务ID
        body.setCommandName("switch");//命令名称  switch_led
        body.withParas("{\"温度\":80}");
        request.withServiceId(service_id);
        try {
            ListPropertiesResponse response = client.listProperties(request);
            System.out.println(response.toString());
            return response;
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return null;
    }


    //http://localhost:8080/iot/properties2?service_id=clzt&device_id=smart_cc
    @RequestMapping("/properties2")
    @ResponseBody
    public ListPropertiesResponse shebeishuxing2(@RequestParam String service_id, String device_id) {
        // 实例化请求对象
        ListPropertiesRequest request = new  ListPropertiesRequest();
        request.setDeviceId("smart_cc");
        request.withServiceId("clzt"); //服务ID   车辆状态
        DeviceCommandRequest body = new DeviceCommandRequest();
        body.setServiceId("clzt");//服务ID
        body.setCommandName("chakan");//命令名称  switch_led
        body.withParas("{\"温度\":80}");
        request.withServiceId(service_id);
        try {
            ListPropertiesResponse response = client.listProperties(request);
            System.out.println(response.toString());
            return response;
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return null;
    }

}
